package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import jdk.security.jarsigner.JarSigner;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;



    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){

        List<Department> departments = departmentService.selectList();
        model.addAttribute("departments",departments);

        PageInfo<Employee> query = employeeService.query(qo);
        model.addAttribute("pageResult",query);
        return "employee/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        employeeService.delete(id);
        return "redirect:/employee/list";
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.selectList();
        List list = roleService.list();
        model.addAttribute("list",list);

        if (id != null){
            Employee employee = employeeService.selectOne(id);
            model.addAttribute("employee",employee);
            List list1 = employeeService.selectEmpAndRole(id);
            model.addAttribute("list1",list1);
            System.out.println();

        }
        return "employee/input";
    }


    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids){
        if (employee.getId()!= null){
            for (Long rId : ids) {
                employeeService.update(employee,rId);
            }
        }else {
            for (Long rId : ids) {
                employeeService.insert(employee, rId);
            }

        }
        return "redirect:/employee/list";
    }


    @RequestMapping("/nopermission")
    public String nopermission(){
        return "common/nopermission";
    }

    @RequestMapping("/error")
    public String error(){
        return "common/error";
    }

    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //创建excel文件
        Workbook wb = employeeService.exportXls(response.getOutputStream());

    }

    @RequestMapping("/inputXls")
    public String inputXml(MultipartFile file) throws IOException {
        employeeService.inputXml(file);
        return "redirect:/employee/list";
    }
}
